﻿using System;
using System.Windows.Input;
using Omnia.DataModels;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Omnia.Library;

namespace Omnia.ViewModels
{
    public class BudgetViewModel : MainViewModel
    {
        #region Properties
        private BudgetInfo itemToAdd = new BudgetInfo();
        public BudgetInfo ItemToAdd
        {
            get { return itemToAdd; }
            set
            {
                itemToAdd = value;
                RaisePropertyChanged(() => ItemToAdd);
            }
        }

        public BudgetInfo CurrentBudget
        {
            get
            {
                if (Database.CollectionOfBudgets.Count(t => t.Month == DateTime.Now.Month && t.Year == DateTime.Now.Year) == 0)
                {
                    return null;
                }
                var item = Database.CollectionOfBudgets.FirstOrDefault(t => t.Month == DateTime.Now.Month && t.Year == DateTime.Now.Year);
                item.Outlay = CurrentMonthTransactionExpense.Sum;
                return item;
            }
        }

        public ObservableCollection<BudgetInfo> OverspendCollection
        {
            get
            {
                if (BudgetsCollection != null && BudgetsCollection.Count > 0)
                {
                    return new ObservableCollection<BudgetInfo>(BudgetsCollection.Where(b => b.Outlay > b.Value));
                }
                return null;
            }
        }

        public ObservableCollection<BudgetInfo> BudgetsCollection
        {
            get
            {
                IEnumerable<BudgetInfo> items = Database.CollectionOfBudgets;
                foreach (BudgetInfo item in items)
                {
                    double o = GetMonthlySum(item.Year, item.Month, Enums.TransactionKindValue.Expense);
                    item.Outlay = o;
                }
                return new ObservableCollection<BudgetInfo>(items);
            }
        }

        public double TotalBudget
        {
            get
            {
                if (BudgetsCollection != null && BudgetsCollection.Count > 0)
                {
                    return BudgetsCollection.Sum(b => b.Value);
                }
                return 0;
            }
        }

        public bool IsEditEnabled
        {
            get
            {
                try
                {
                    int year = DateTime.Now.Year;
                    int month = DateTime.Now.Month;

                    return Database.CollectionOfBudgets.Count(b => b.Year == year && b.Month == month) > 0;
                }
                catch { return false; }
            }
        }
        #endregion

        #region Method
        private void Insert(BudgetInfo entity)
        {
            Database.CollectionOfBudgets.InsertOnSubmit(entity);
            Save();
            RaisePropertyChanged(() => BudgetsCollection);
            RaisePropertyChanged(() => IsEditEnabled);
            RaisePropertyChanged(() => CurrentBudget);
            RaisePropertyChanged(() => AddBudgetCommand);
            RaisePropertyChanged(() => UpdateBudgetCommand);
            RaisePropertyChanged(() => TotalBudget);
            RaisePropertyChanged(() => OverspendCollection);
        }

        private void Update(BudgetInfo entity)
        {
            var item = Database.CollectionOfBudgets.FirstOrDefault(b => b.Guid == CurrentBudget.Guid);
            item.Value = entity.Value;
            Save();

            RaisePropertyChanged(() => CurrentBudget);
            RaisePropertyChanged(() => TotalBudget);
        }

        #endregion

        #region Commands
        public ICommand AddBudgetCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return !IsEditEnabled; },
                    ExecuteCallback = delegate {
                        Insert(ItemToAdd);
                    }
                };
            }
        }

        public ICommand UpdateBudgetCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return IsEditEnabled; },
                    ExecuteCallback = delegate
                    {
                        Update(ItemToAdd);
                        Refresh();
                    }
                };
            }
        }
        #endregion
    }
}
